<?php

namespace app\crm\controller\clues;

use app\common\controller\CrmController;
use app\common\service\BaseService;
use app\crm\model\CrmClues;
use app\crm\model\CrmRecord;
use app\crm\traits\Curd;

class Index extends CrmController
{
    protected $validate = 'clues\Clues';   //线索表验证器
    protected $model = 'CrmClues';          //线索数据模型  


    use Curd;

    public function initialize()
    {
        parent::initialize();
        if (in_array($this->request->action(), ['add', 'edit', 'index', 'recycle'])) {
            $this->assign('campusList', BaseService::getCampusList());  // 校区
            $this->assign('courseList', BaseService::getCourseList());  // 课程 
            $this->assign('sourceList', BaseService::getSourceList());  // 来源
        }
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        // 1.验证请求方式
        if ($this->request->isPost()) {
            // 2.获取请求数据
            $post = $this->request->post();
            $post['create_user_id'] = session('admin.id');
            $post['owner_user_id'] = session('admin.id');
            $validate = new $this->validate();
            if ($validate->check($post) == false) {
                return $this->error($validate->getError());
            }
            $result = $this->model->allowField(true)->save($post);

            if (!$result) {
                return $this->error('保存失败');
            }
            return $this->success('保存成功');
        }
        return $this->fetch('form');
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $where[] =  ['owner_user_id', '<>', 0];
            $where[] =  ['create_user_id', '=', session('admin.id')];
            $count = $this->model::with('course,campus,createUser,ownerUser')
                ->where($where)
                ->count();
            $list = $this->model::with('course,campus,createUser,ownerUser')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 跟进
     */
    public function record()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return $this->error('缺少id参数1111');
        }
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = CrmRecord::with('createUser')->where('types_id',$id)->where($where)
                ->count();
            $list = CrmRecord::with('createUser')->where('types_id',$id)->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
            $this->assign('clues', CrmClues::get($id));
            $this->assign('record_methods', BaseService::getRecordMethodList());
        return $this->fetch();
    }

}
